package com.example.studentmanagmentsystem;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import org.litepal.LitePal;
import org.litepal.Operator;

import java.util.ArrayList;
import java.util.List;

public class ScoreShowActivity extends AppCompatActivity {

    List<Score> scoreList;

    public List<Score> getScoreList() {
        return scoreList;
    }

    public void setScoreList(List<Score> scoreList) {
        this.scoreList = scoreList;
    }

    private String studentName;

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score_show);

        studentName = getIntent().getStringExtra("StudentName");
        List<Student> students = LitePal.findAll(Student.class);
        List<Course> courses = LitePal.findAll(Course.class);
        List<Score> scores = LitePal.findAll(Score.class);

        if (studentName == null) {

            RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view_score);
            LinearLayoutManager layoutManager = new LinearLayoutManager(ScoreShowActivity.this);
            recyclerView.setLayoutManager(layoutManager);//初始化布局信息
            ScoreAdapter adapter = new ScoreAdapter(scores);
            adapter.setOnDeleteClickListener(new ScoreAdapter.OnDeleteClickListener() {
                @Override
                public void onDeleteClick(View view, int position) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(ScoreShowActivity.this);
                    builder.setTitle("删除学生成绩信息").setMessage("你确定要删除吗?").setNegativeButton("取消", null);
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Operator.deleteAll(Score.class, "studentid = ? and courseid = ?", scores.get(position).getStudentId() + "", scores.get(position).getCourseId() + "");
                            Toast.makeText(ScoreShowActivity.this, "成功删除该成绩信息 =>" + scores.get(position).getScore(), Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(ScoreShowActivity.this, ScoreShowActivity.class);
                            startActivity(intent);
                        }
                    });
                    builder.show();
                }
            });

            adapter.setmOnItemClickListener(new ScoreAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    String SName = "";
                    String CName = "";

                    for (int i = 0; i < students.size(); i++) {
                        if (scores.get(position).getStudentId() == students.get(i).getId()) {
                            SName = students.get(i).getName();
                        }
                    }
                    for (int i = 0; i < courses.size(); i++) {
                        if (scores.get(position).getCourseId() == courses.get(i).getId()) {
                            CName = courses.get(i).getName();
                        }
                    }

                    EditText studentScore = new EditText(ScoreShowActivity.this);
                    AlertDialog.Builder builder = new AlertDialog.Builder(ScoreShowActivity.this);
                    builder.setTitle("成绩修改").setView(studentScore).setMessage("你确定要修改 " + SName + " 的 " + CName + " 成绩吗 ?")
                            .setNegativeButton("Cancel", null);
                    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Score updateScore = new Score();
                            updateScore.setScore(Double.parseDouble(studentScore.getText().toString()));
                            updateScore.updateAll("studentid = ? and courseid = ?", scores.get(position).getStudentId() + "", scores.get(position).getCourseId() + "");
                            //scores.get(position).setScore(Double.parseDouble(studentScore.getText().toString()));
                            Toast.makeText(ScoreShowActivity.this, "成绩修改为了" + studentScore.getText().toString(), Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(ScoreShowActivity.this, ScoreShowActivity.class);
                            startActivity(intent);
                        }

                    });
                    builder.show();

                }
            });

            recyclerView.setAdapter(adapter);

        }else{//不为空则...
            int studentId = 0;
            for (int i = 0; i < students.size(); i++) {//找出对应的学生
                if (students.get(i).getName().equals(studentName)) {
                    studentId = students.get(i).getId();
                }
            }

            List<Score> resultScore = new ArrayList<>();
            for (int i = 0; i < scores.size(); i++) {//并把查找出来的信息放到一个新链表中
                if (scores.get(i).getStudentId() == studentId) {
                    resultScore.add(scores.get(i));
                }
            }
            if (resultScore.size() != 0) {//如果新链表非空则显示
                RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view_score);
                LinearLayoutManager layoutManager = new LinearLayoutManager(ScoreShowActivity.this);
                recyclerView.setLayoutManager(layoutManager);//初始化布局信息
                ScoreAdapter adapter = new ScoreAdapter(resultScore);
                adapter.setOnDeleteClickListener(new ScoreAdapter.OnDeleteClickListener() {
                    @Override
                    public void onDeleteClick(View view, int position) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(ScoreShowActivity.this);
                        builder.setTitle("删除学生成绩信息").setMessage("你确定要删除吗?").setNegativeButton("取消", null);
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Operator.deleteAll(Score.class, "studentid = ? and courseid = ?", resultScore.get(position).getStudentId() + "", resultScore.get(position).getCourseId() + "");
                                Toast.makeText(ScoreShowActivity.this, "成功删除该成绩信息 =>" + resultScore.get(position).getScore(), Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(ScoreShowActivity.this, ScoreShowActivity.class);
                                startActivity(intent);
                            }
                        });
                        builder.show();
                    }
                });

                adapter.setmOnItemClickListener(new ScoreAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        String SName = "";
                        String CName = "";

                        for (int i = 0; i < students.size(); i++) {
                            if (resultScore.get(position).getStudentId() == students.get(i).getId()) {
                                SName = students.get(i).getName();
                            }
                        }
                        for (int i = 0; i < courses.size(); i++) {
                            if (resultScore.get(position).getCourseId() == courses.get(i).getId()) {
                                CName = courses.get(i).getName();
                            }
                        }

                        EditText studentScore = new EditText(ScoreShowActivity.this);
                        AlertDialog.Builder builder = new AlertDialog.Builder(ScoreShowActivity.this);
                        builder.setTitle("成绩修改").setView(studentScore).setMessage("你确定要修改 " + SName + " 的 " + CName + " 成绩吗 ?")
                                .setNegativeButton("Cancel", null);
                        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                Score updateScore = new Score();
                                updateScore.setScore(Double.parseDouble(studentScore.getText().toString()));
                                updateScore.updateAll("studentid = ? and courseid = ?", resultScore.get(position).getStudentId() + "", resultScore.get(position).getCourseId() + "");
                                //scores.get(position).setScore(Double.parseDouble(studentScore.getText().toString()));
                                Toast.makeText(ScoreShowActivity.this, "成绩修改为了" + studentScore.getText().toString(), Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(ScoreShowActivity.this, ScoreShowActivity.class);
                                startActivity(intent);
                            }

                        });
                        builder.show();

                    }
                });

                recyclerView.setAdapter(adapter);
            }


        }
    }

}